package com.system.template.core.pdf;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PdfPagingSealUtil {

	/**
	 * 切割图片
	 * @param imgPath  原始图片路径
	 * @param n 切割份数
	 * @return  itextPdf的Image[]
	 * @throws IOException
	 * @throws BadElementException
	 */
	public static Image[] subImages(String imgPath,int n) throws IOException, BadElementException {
		Image[] nImage = new Image[n];
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		BufferedImage img = ImageIO.read(new File(imgPath));
		/*//控制图片大小
		if (pdfWatermark.getWidth() != null && pdfWatermark.getHeight() != null) {
			// 自定义水印的大小
			img = resizeBufferedImage(img, pdfWatermark.getWidth().intValue(), pdfWatermark.getHeight().intValue(), false);
		}*/
		int h = img.getHeight();
		int w = img.getWidth();

		int sw = w/n;
		for(int i=0;i<n;i++){
			BufferedImage subImg;
			if(i==n-1){//最后剩余部分
				subImg = img.getSubimage(i * sw, 0, w-i*sw, h);
			}else {//前n-1块均匀切
				subImg = img.getSubimage(i * sw, 0, sw, h);
			}

			ImageIO.write(subImg,imgPath.substring(imgPath.lastIndexOf('.')+1),out);
			nImage[i] = Image.getInstance(out.toByteArray());
			out.flush();
			out.reset();
		}
		return nImage;
	}

	/**
	 *  盖骑缝章
	 *
	 * @param infilePath    原PDF路径
	 * @param outFilePath    输出PDF路径
	 * @param pdfWatermark    章图片对象
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void stamperCheckMarkPDF(String infilePath, String outFilePath, PdfWatermark pdfWatermark) throws IOException, DocumentException {
		String pdfWatermarkPath = pdfWatermark.getPath();
		PdfReader reader = new PdfReader(infilePath);//选择需要印章的pdf
		PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf

		Rectangle pageSize = reader.getPageSize(1);//获得第一页
		float height = pageSize.getHeight();
		float width  = pageSize.getWidth();

		int nums = reader.getNumberOfPages();
		//生成骑缝章切割图片
		Image[] nImage = subImages(pdfWatermarkPath, nums);


		for(int n=1;n<=nums;n++) {
			PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章
			Image img = nImage[n-1];//选择图片
			//            img.setAlignment(1);
			/*//控制图片大小
			if (pdfWatermark.getWidth() != null && pdfWatermark.getHeight() != null) {
				// 自定义水印的大小
				img.scaleAbsolute(pdfWatermark.getWidth(), pdfWatermark.getHeight());
			}*/
			img.setAbsolutePosition(width-img.getWidth(), height/2-img.getHeight()/2);//控制图片位置
			over.addImage(img);
		}
		stamp.close();
	}


	/*public static void main(String[] args) throws IOException, DocumentException {
		String infilePath = "D:\\git\\micro-service\\ms-core\\comm\\plug-template\\src\\test\\java\\pdf.pdf";
		String outfilePaht = "D:\\git\\micro-service\\ms-core\\comm\\plug-template\\src\\test\\java\\pdf23.pdf";
		String watermarkPath = "D:\\git\\micro-service\\ms-core\\comm\\plug-template\\src\\test\\java\\watermark.jpg";
		PdfWatermark pdfWatermark = new PdfWatermark(1, watermarkPath, 140f, 140f, 100f, 100f);
		stamperCheckMarkPDF(infilePath, outfilePaht, pdfWatermark);
	}*/
}
